package com.micromall.common.base;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.micromall.common.exception.BusiException;
import com.micromall.common.exception.ParameterException;
import com.micromall.common.result.Result;
import com.micromall.common.utils.JsonResult;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * @description：基础 controller
 * @author：Sierou Wang
 * @date：2015/10/1 14:51
 */
public abstract class BaseController {
    // 控制器本来就是单例，这样似乎更加合理
    protected Logger logger = LogManager.getLogger(getClass());

    /** 基于@ExceptionHandler异常处理 */
    // @ExceptionHandler
    public String exp(HttpServletRequest request, Exception ex) {

        request.setAttribute("ex", ex);

        // 根据不同错误转向不同页面
        if(ex instanceof BusiException) {
            request.setAttribute("error", ex.getMessage());
            return "error/error-biz";
        }else if(ex instanceof ParameterException) {
            return "error-parameter";
        } else {
            return "error/500";
        }
    }



/*    *//**
     * 获取当前登录用户对象
     * @return {ShiroUser}
     *//*
    public ShiroUser getShiroUser() {
        return (ShiroUser) SecurityUtils.getSubject().getPrincipal();
    }

    *//**
     * 获取当前登录用户id
     * @return {Long}
     *//*
    public Long getUserId() {
        return this.getShiroUser().getId();
    }

    *//**
     * 获取当前登录用户名
     * @return {String}
     *//*
    public String getStaffName() {
        return this.getShiroUser().getName();
    }*/

    /**
     * ajax失败
     * @param msg 失败的消息
     * @return {Object}
     */
    public Object renderError(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        return result;
    }

    /**
     * ajax成功
     * @return {Object}
     */
    public Object renderSuccess() {
        Result result = new Result();
        result.setSuccess(true);
        return result;
    }

    /**
     * ajax成功
     * @param msg 消息
     * @return {Object}
     */
    public Object renderSuccess(String msg) {
        Result result = new Result();
        result.setSuccess(true);
        result.setMsg(msg);
        return result;
    }

    /**
     * ajax成功
     * @param obj 成功时的对象
     * @return {Object}
     */
    public Object renderSuccess(Object obj) {
        Result result = new Result();
        result.setSuccess(true);
        result.setObj(obj);
        return result;
    }

    /**
     *
     * @return
     */
    public Object write2Json(Object data,Boolean isSuccess, String msg,String callback) {
         JsonResult jsonResult = new JsonResult<Object>();
         jsonResult.setData(data);
         jsonResult.setMsg(msg);
         jsonResult.setStatus(isSuccess? com.micromall.common.utils.JsonResult.SUCCESS.getCode(): com.micromall.common.utils.JsonResult.FAIL.getCode());
         if (StringUtils.isNotBlank(callback)) {
            return String.format("%s(%s)", callback, JSONObject.toJSONString(jsonResult));
         }
        return jsonResult;
    }
    
    public <T> Page<T> getPage(int current, int size, String id_sort, String order){
        Page<T> page = new Page<T>(current, size, id_sort);
        if ("desc".equals(order)) {
            page.setAsc(false);
        } else {
            page.setAsc(true);
        }
        return page;
    }
    
    public <T> com.micromall.common.utils.PageInfo pageToPageInfo(Page<T> page) {
        com.micromall.common.utils.PageInfo pageInfo = new com.micromall.common.utils.PageInfo();
        pageInfo.setRows(page.getRecords());
        pageInfo.setTotal(page.getTotal());
        pageInfo.setOrder(page.getOrderByField());
        return pageInfo;
    }

}
